package com.survey.system.controller;

import com.survey.common.core.controller.BaseController;
import com.survey.common.core.domain.AjaxResult;
import com.survey.common.core.domain.entity.SysUser;
import com.survey.common.utils.DateUtils;
import com.survey.system.domain.SurFeedback;
import com.survey.system.domain.SurIssue;
import com.survey.system.domain.SurIssueType;
import com.survey.system.domain.SurSchool;
import com.survey.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Stream;

@RestController
@RequestMapping("/system/dashboard")
public class SurDashboardController extends BaseController {
    @Autowired
    private ISurSchoolService surSchoolService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISurIssueService surIssueService;
    @Autowired
    private ISurFeedbackService surFeedbackService;
    @Autowired
    private ISurIssueTypeService surIssueTypeService;
    @GetMapping("/data")
    public AjaxResult getData() {
        AjaxResult stats = new AjaxResult();
        AjaxResult chartData = new AjaxResult();
        AjaxResult data = new AjaxResult();

        // 获取统计数据
        stats.put("schoolCount", surSchoolService.selectSurSchoolList(new SurSchool()).size());
        stats.put("userCount", sysUserService.selectUserList(new SysUser()).size());
        stats.put("issueCount", surIssueService.selectSurIssueList(new SurIssue()).size());
        stats.put("feedbackCount", surFeedbackService.selectSurFeedbackList(new SurFeedback()).size());
        data.put("stats", stats);

        // 获取用户的图表数据
        AjaxResult userChart = new AjaxResult();
        userChart.put("dates", getDateList());
        userChart.put("values", sysUserService.countByDate());
        chartData.put("users", userChart);

        // 获取学校的图表数据
        AjaxResult schoolChart = new AjaxResult();
        schoolChart.put("dates", getDateList()); 
        schoolChart.put("values", surSchoolService.countByDate());
        chartData.put("schools", schoolChart);

        // 获取问题的图表数据
        AjaxResult issueChart = new AjaxResult();
        issueChart.put("dates", getDateList()); 
        issueChart.put("values", surIssueService.countByDate());
        chartData.put("issues", issueChart);

        // 获取反馈的图表数据
        AjaxResult feedbackChart = new AjaxResult();
        feedbackChart.put("dates", getDateList()); 
        feedbackChart.put("values", surFeedbackService.countByDate());
        chartData.put("feedback", feedbackChart);

        // 将图表数据放入返回数据中
        data.put("chartData", chartData);

        return AjaxResult.success(data);
    }

    private List<String> getDateList() {
        List<String> dateList = new ArrayList<>();
        LocalDate now = LocalDate.now();
        for (int i = 0; i < 7; i++){
            LocalDate localDate = now.minusDays(i);
            dateList.add(localDate.toString());
        }
        return dateList;
    }

    @GetMapping("/schoolList")
    public AjaxResult test() {
        List<SurSchool> surSchools = surSchoolService.selectSurSchoolList(new SurSchool());
        for (SurSchool surSchool : surSchools) {
            SurIssue surIssue = new SurIssue();
            surIssue.setSchoolId(surSchool.getSchoolId());
            List<SurIssue> surIssues = surIssueService.selectSurIssueList(surIssue);

            // 过滤出 issue_status 为 0 和 1 的列表
            Integer countStatus0 = Math.toIntExact(surIssues.stream().filter(issue -> issue.getIssueStatus() == 0).count());
            Integer countStatus1 = Math.toIntExact(surIssues.stream().filter(issue -> issue.getIssueStatus() == 1).count());

            surSchool.setIssueCount(surIssues.size());
            surSchool.setPendingCount(countStatus0);
            surSchool.setResolvedCount(countStatus1);
        }
        return success(surSchools);
    }

    @GetMapping("/schoolData")
    public AjaxResult schoolData() {
        AjaxResult data = new AjaxResult();
        Long userId = getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (sysUser.getSchoolId() != null) {
            Long schoolId = sysUser.getSchoolId();
            SurIssue surIssue = new SurIssue();
            surIssue.setSchoolId(schoolId);
            List<SurIssue> surIssues = surIssueService.selectSurIssueList(surIssue);
            data.put("total", surIssues.size());
            // 过滤出 issue_status 为 0 和 1 的列表
            Integer countStatus0 = Math.toIntExact(surIssues.stream().filter(issue -> issue.getIssueStatus() == 0).count());
            Integer countStatus1 = Math.toIntExact(surIssues.stream().filter(issue -> issue.getIssueStatus() == 1).count());
            data.put("solved",countStatus1);
            data.put("unsolved", countStatus0);

            // 获取今天的问题列表
            Integer todayIssues = surIssueService.selectTodaySurIssue();
            data.put("todayNew", todayIssues);


            Integer surFeedbacks = surFeedbackService.selectTodaySurFeedback();
            data.put("todaySolved", surFeedbacks);

            //获取问题类型列表
            SurIssueType surIssueType = new SurIssueType();
            List<SurIssueType> surIssueTypes = surIssueTypeService.selectSurIssueTypeList(surIssueType);

            // 实例化一个Map列表用于存储问题类型和数量
            List<Map<String, Integer>> typeCounts = new ArrayList<>();
            for (SurIssueType issueType : surIssueTypes) {
                String typeName = issueType.getTypeName();
                Long typeId = issueType.getTypeId();
                // 创建一个SurIssue对象，用于查询问题类型对应的数量
                SurIssue queryParams = new SurIssue();
                // 设置查询条件为问题类型ID和学校ID
                queryParams.setTypeId(typeId);
                queryParams.setSchoolId(schoolId);
                // 获取问题类型对应的数量
                int size = surIssueService.selectSurIssueList(queryParams).size();
                // 创建一个Map，用于存储问题类型和数量
                Map<String, Integer> typeCount = new HashMap<>();
                typeCount.put(typeName, size);
                typeCounts.add(typeCount);
            }
            data.put("issueTypes", typeCounts);
        }
        return success(data);
    }

}